#include "pthreadPool.h"

void * threadFunc(void *arg){
    threadPool_t *pthreadPool = (threadPool_t*)arg;
    while(1){
        pthread_mutex_lock(&pthreadPool->mutex);
        while(pthreadPool->flag == 0 && pthreadPool->taskQueue.queueSize <= 0){
            pthread_cond_wait(&pthreadPool->cond,&pthreadPool->mutex);
        }
        int netFd = pthreadPool->taskQueue.pFront->netFd;
        int userId = pthreadPool->taskQueue.pFront->code;
        if(pthreadPool->flag == 1){
            printf("【Client %d 】:Go go go.\n",netFd);
            pthread_mutex_unlock(&pthreadPool->mutex);
            pthread_exit(NULL);
        }
        printf("【Client】:I am worker.netfd = %d\n",netFd);
        deQueue(&pthreadPool->taskQueue);
        pthread_mutex_unlock(&pthreadPool->mutex);
        //do_sth
        handleLongCmd(netFd,userId);

        printf("Done.\n");
        close(netFd);
    }
    return NULL;
}
int makeWorker(threadPool_t *pthreadPool){
    for(int i = 0;i < pthreadPool->tidArr.workerNum;++i){
        pthread_create(&pthreadPool->tidArr.pthreadTid[i],NULL,threadFunc,pthreadPool);
    }
    printf("Here are %d workers.\n",pthreadPool->tidArr.workerNum);
    return 0;
}
